package com.itheima.dao;

import com.itheima.domian.Permission;
import com.itheima.domian.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IRoleDao {

    // 根据UserID查询权限
    @Select("select * from role   where  id  in  (select  t.roleid from Users_role  t where t.USERID = #{UserId} )")
    @Results({
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "roleName"),
            @Result(property = "roleDesc", column = "roleDesc"),
            @Result(property = "permissions", column = "id", javaType = java.util.List.class, many = @Many(select = "com.itheima.dao.IPermissionDao.findPermissionByRoleId"))

    })
    public List<Role> findRoleByUsersId(String UserId) throws Exception;


    @Select("select * from role")
    public List<Role> findAll() throws Exception;

    @Insert("insert into role (ROLENAME, ROLEDESC) values ( #{roleName},#{roleDesc} )")
    void save(Role role) throws Exception;

    @Select("select * from  role where id =#{roleId}")
    Role findById(String roleId) throws  Exception;

    @Select("select * from  permission where id not in(select permissionid from role_permission where roleid = #{roleId})")
    List<Permission> findOtherPermission(String roleId) throws Exception;

    @Insert("insert into role_permission(ROLEID,PERMISSIONID)  values (#{roleId} , #{permissionId} )")
    void addRoleToPermission(@Param("roleId") String roleId, @Param("permissionId") String permissionId) throws Exception;
}
